<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
   /**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
class Pages extends CI_Controller {
 
     function __construct()
     {
        parent::__construct();
        $this->load->database();
        $this->load->library(array('ion_auth','form_validation','pagination'));
        $this->load->helper(array('form', 'url','html','country'));
        $this->data = $this->ion_auth->user()->row();
		 
     }
     public function home()
     {
		$page = 'home';
                //$this->data = $this->ion_auth->user()->row();
                //$data = $this->ion_auth->user()->row();
                //$email = $data->email;
                //exit;
		if ( ! file_exists('application/frontend/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
		//$data['title'] = ucfirst($page); // Capitalize the first letter
		
		$this->load->view('templates/header', $this->data);
		$this->load->view('pages/'.$page, $this->data);
		$this->load->view('templates/footer', $this->data);
	}
	
     public function abonner()
     {
		$page = 'abonner';
		if ( ! file_exists('application/frontend/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
		$data['title'] = ucfirst($page); // Capitalize the first letter
		
		$this->load->view('templates/header', $this->data);
		$this->load->view('pages/'.$page, $this->data);
		$this->load->view('templates/footer', $this->data);
	}
	
     public function marques()  
     {
		$page = 'marques';
		if ( ! file_exists('application/frontend/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
		$data['title'] = ucfirst($page); // Capitalize the first letter
		
		$this->load->view('templates/header', $this->data);
		$this->load->view('pages/'.$page, $this->data);
		$this->load->view('templates/footer', $this->data);
	}
	
     public function partenaires()
     {
		$pages = 'nos_partenaires';
                if ( ! file_exists('application/frontend/views/pages/'.$pages.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
		$data['title'] = ucfirst($pages); // Capitalize the first letter
                $this->load->model("partenaire_model");
                
                $config = array();
                $config["base_url"] = site_url('pages/partenaires');
                $config["total_rows"] = $this->partenaire_model->record_count();
                $config["per_page"] = 5;
                $config["uri_segment"] = 3;
                $config['use_page_numbers'] = TRUE;
                //$choice = $config["total_rows"] / $config["per_page"];

                //$config["num_links"] = round($choice);

                $this->pagination->initialize($config);

                //echo $this->uri->segment(3);exit;
                $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
                if ($page > 0)
                    $page = $page - 1;
                $data["results"] = $this->partenaire_model->fetch_partenaire($config["per_page"], $page * $config["per_page"]);
                $data["links"] = $this->pagination->create_links();
                
		$this->load->view('templates/header', $this->data);
		$this->load->view('pages/nos_partenaires', $data);
		$this->load->view('templates/footer', $this->data);
	}
	
     public function offre_privilege()
     {
		$page = 'offre_privilege';
		if ( ! file_exists('application/frontend/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
		$data['title'] = ucfirst($page); // Capitalize the first letter
		
		$this->load->view('templates/header', $this->data);
		$this->load->view('pages/'.$page, $this->data);
		$this->load->view('templates/footer', $this->data);
	}
     
     public function blog()
     {
		$page = 'blog';
		if ( ! file_exists('application/frontend/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
		$data['title'] = ucfirst($page); // Capitalize the first letter
		
		$this->load->view('templates/header', $this->data);
		$this->load->view('pages/'.$page, $this->data);
		$this->load->view('templates/footer', $this->data);
	}

     public function compte()
     {
        $this->load->model('member');
        $page = 'mon_compte';
         if (!$this->ion_auth->logged_in())
        {
            if ( ! file_exists('application/frontend/views/pages/'.$page.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }
            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->helper(array('form', 'url'));

            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|min_length[6]');
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
            $this->form_validation->set_rules('prenom', 'Prenom', 'trim|required');
            $this->form_validation->set_rules('civilite', 'Civilite', 'trim|required');
            $this->form_validation->set_rules('nom', 'Nom Confirmation', 'trim|required');
            $this->form_validation->set_rules('adresse', 'Adresse', 'trim|required');
            $this->form_validation->set_rules('codepostal', 'Code Postal', 'trim|required|min_length[5]');
            $this->form_validation->set_rules('ville', 'Ville', 'trim|required');
            $this->form_validation->set_rules('pays', 'Pays', 'trim|required');
            $this->form_validation->set_rules('fixe', 'Fixe', 'trim|required|is_numeric|min_length[10]|max_length[10]');
            $this->form_validation->set_rules('portable', 'Portable', 'trim|required|is_numeric|min_length[10]|max_length[10]');
            $this->form_validation->set_rules('datenaissance', 'Date de naissance', 'callback_check_datenaissance');



            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('templates/header', $this->data);
                $this->load->view('pages/'.$page, $this->data);
                $this->load->view('templates/footer', $this->data);
            }
            else
            {
                if ($this->member->insertMember()){
                    $this->load->view('templates/header', $this->data);
                    $this->load->view('pages/activation', $this->data);
                    $this->load->view('templates/footer', $this->data);
                }
            }
        }elseif($this->ion_auth->in_group(2)){
                    $this->load->view('templates/header', $this->data);
                    $this->load->view('membre/home', $this->data);
                    $this->load->view('templates/footer', $this->data);
        }elseif($this->ion_auth->in_group(3)){
                    //$this->load->view('templates/header', $this->data);
                    //$this->load->view('partenaire/main', $this->data);
                    //$this->load->view('templates/footer', $this->data);
             redirect('partenaire/information');
            
        }
    }
    
    public function check_datenaissance($date)
	{
            if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $date)) {
                if(checkdate(substr($date, 3, 2), substr($date, 0, 2), substr($date, 6, 4)))
                    return TRUE;
                else
                    return FALSE;
                } 
                else {
                    return FALSE;
                }
	}
}
?>